<?php
session_start();
if(isset($_SESSION['admin_id'])) {
$mes="";
if (isset($_SESSION['mes'])) {
    $mes = $_SESSION['mes'];
    $_SESSION['mes']="";
}
include ("../include/menu.php");
include ("../include/dbFunctions.php");
//$event_id=$_GET['id'];
$event_id=$_SESSION['event_id'];
$query = "SELECT * FROM student WHERE event_id=$event_id";
//$query = "SELECT * FROM student WHERE event_id=1";
$student_array = executeSelectQuery($query);
$query2="SELECT event_name FROM event WHERE event_id=$event_id";
$event_array = executeSelectQuery($query2);
$today=date("'m/d/Y'");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery.dataTables.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery.dataTables.editable.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery.jeditable.js"></script>
        <script type="text/javascript" src="../media/js/jquery/jquery-ui-1.8.13.custom.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery.validate.js"></script>

        <script type="text/javascript" src="../media/js/jquery/addInPlace.js"> </script>

        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />
        <link type="text/css" href="../media/css/jquery-ui-1.8.13.custom.css" rel="stylesheet" />

        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            function addNewStudent(){
                var addURL = "doAddStudent.php"
                //Get data from the form
                var student_name = $("input#student_name").val();
                var student_birthday = $("input#student_birthday").val();
                var student_phone = $("input#student_phone").val();

                var dataString = 'student_name='+student_name+'&student_birthday='+student_birthday+"&student_phone="+student_phone;

                $.ajax({
                    type:"POST",
                    url:addURL,
                    data: dataString,
                    success: function(data){
                        if(data == 1){
                            alert("Add Successfully!")
                            window.location.reload();
                        }else{
                            alert("An error was occured! Could not insert new student!");
                        }
                    }
                });

            }//End addNewStudent()

            //Using plugin to load data from loadStudent.php
            $(document) .ready(function(){
                $('#addNewStudent').dialog({
                    autoOpen: false,
                    show: "blind",
                    hide: "explode",
                    width: 550,
                    height: 300,
                    modal:true,
                    buttons:{
                        "Submit": function(){
                            options = {
                                rules:{
                                    name:"required",
                                    birthday:"required",
                                    phone:"required"
                                }
                            }
                            if($('#addStudentForm').validate(options).form()==true){
                                addNewStudent();
                            }

                        },
                        "Cancel": function(){
                            $(this).dialog("close");

                        },
                        "Reset": function(){
                            $('#addStudentForm').validate().resetForm();
                            $('#addStudentForm')[0].reset();
                        }

                    }//End buttons
                });

                $('#addNewStudentFormBtn').click(function(){
                    $('#addNewStudent').dialog("open");

                });

            });//End $(document).ready(function()
            $(document).ready(function() {
                $('#admintable').dataTable({
                    "sPaginationType": "full_numbers"
                });



            } );
        </script>
        <script language="JavaScript">
            function toggle1(source) { //for select all checkbox :)
                checkboxes = document.getElementsByName('id[]');
                for each(var checkbox in checkboxes)
                checkbox.checked = source.checked;
            }
        </script>


    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
        <?php
        top_outer();
        ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                <?php
                nav_right();
                ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                        <?php
                        navigation();
                        ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>Student List</h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->

                                <div id="table-content">
                                    <!--  Can put table start from here within this div-->
                                    <h3><?php echo $event_array[0]['event_name']?></h3>
                                    <form name="delete_student" method="post" action="studentDelete.php" id ="student">
                                        <?php echo "<b><font color =red>$mes </font></b>" ?>
                                        <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                            <thead>
                                                <tr>
                                                    <th><input type="checkbox" onClick="toggle1(this)" /></th>
                                                    <th>No.</th>
                                                    <th>Student Name</th>
                                                    <th>Student birthday</th>
                                                    <th>Age</th>
                                                    <th>Student phone contact</th>
                                                    <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>

                                                <?php
                                                if ($student_array!=null) {
                                                    for ($i=0;$i<count($student_array);$i++) {
                                                        $no = $i+1;
                                                        $id = $student_array[$i]['student_id'];
                                                        $name =$student_array[$i]['student_name'];
                                                        $birthday =$student_array[$i]['student_birthday'];
                                                        $phone = $student_array[$i]['student_phone'];
                                                        list($day,$month,$year) = explode("/",$birthday);
                                                        $year_diff = date("Y") - $year;
                                                        $month_diff = date("m") - $month;
                                                        $day_diff = date("d") - $day;
                                                        if ($month_diff < 0) $year_diff--;
                                                        elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;

                                                        (10 * $i % 2 == 0 ? $class = 'even' : 'odd');
                                                        echo "<tr class='$class' id='" . $id . "'>";
                                                        echo <<<HERE
                                    <td><input type = "checkbox" name ="id_student[]" value ="$id" ></input></td>
HERE;
                                                        echo "<td>$id</td>";
                                                        echo "<td>$name</a></td>";
                                                        echo "<td>$birthday</td>";
                                                        echo "<td>$year_diff</td>";
                                                        echo "<td>$phone</td>";
                                                        echo "<td><a href='studentDelete.php?id=".$id."'>Delete</a></td>";
                                                        echo "</tr>";
                                                    }

                                                }else {

                                                    echo "<b><font color =red>The event has no students</b></font>";
                                                }

                                                ?>
                                            </tbody>
                                        </table>
                                        <table>
                                            <tr>
                                                <td><input type="button" class="form-back" value="Back" onClick="javascript:history.go(-1)"/></td>
                                                <td>
                                                    <div id="addNewStudentFormBtn">
                                                        <input type="button" value="&nbsp" class="add-student"/>
                                                    </div><!-- End of #addNewStudentFormBtn!-->
                                                </td>
                                                <td><input type="submit" name="submit" value="Delete" class="form-delete" onClick="if (confirm('Are you sure you want to delete?')) commentDelete(1); return false"/><br/></td>

                                            </tr>
                                        </table>


                                    </form>


                                    <div id="addNewStudent" title="Add new student">

                                        <form name="addStudentForm" id="addStudentForm" action="">

                                            <table>
                                                <tr>
                                                    <th>Student Name</th>
                                                    <td><input type="text" id="student_name" name="name"/></td>
                                                </tr>
                                                <tr>
                                                    <th>Student Birthday DD/MM/YYYY</th>
                                                    <td><input type="text" id="student_birthday" name="birthday"/></td>
                                                </tr>
                                                <tr>
                                                    <th>Student Phone</th>
                                                    <td><input type="text" id="student_phone" name="phone"/></td>
                                                </tr>

                                            </table>

                                        </form>

                                    </div><!--End of#addNewStudentForm!-->


                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
        <?php
        footer();
        ?>

    </body>
</html>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>